
<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" :value="inputValue" @change="inputChange" @keyup.enter="addItemToList" />

    <a-list bordered :dataSource="infoList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="(e)=>{ cbChangeState(e,item.id)}">{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="removeById(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{unDoneLength}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="viewKey ==='all'? 'primary':'default'"  @click="changeList('all')">全部</a-button>
          <a-button :type="viewKey ==='undone'? 'primary':'default'"   @click="changeList('undone')">未完成</a-button>
          <a-button :type="viewKey ==='done'? 'primary':'default'"  @click="changeList('done')">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="cleanAllDone">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
// 按需导入函数 获取vuex中的state的值
import {mapState,mapGetters} from 'vuex'
export default {
  name: 'app',
  data() {
    return {
    }
  },created(){
    this.$store.dispatch('getList');
  },
  computed:{
    ...mapState(['inputValue','viewKey']),
    ...mapGetters(['unDoneLength','infoList'])
  },methods:{
    //监听输入框值变化
    inputChange(e){
      //console.log(e.target.value);
      this.$store.commit('setInputValue',e.target.value)
    },
    // 新增条目
    addItemToList(){
      if(this.inputValue.trim().length==0){
         return this.$message.warning('文本框内容不能为空！')
      }
      this.$store.commit('addItem')
    },
    // 根据id删除item
    removeById(id){
      this.$store.commit('removeItem',id);
    },
    // 复选框的值改变
    cbChangeState(e,id){
      //console.log(e.target.checked,id);
      const param = {
        id:id,
        done:e.target.checked
      }
      this.$store.commit('stateChange',param)
    },
    // 清除已完成
    cleanAllDone(){
      this.$store.commit('clearAllDone')
    },
  // 面板切换
    changeList(type){
      //console.log(type);
      this.$store.commit('changeView',type)
    }
    
  }
}
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
